package jbrowser.session;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.Attributes;
import jbrowser.JBrowser;
import jbrowser.api.ClassLoadingException;

public class AppSession implements Session {

    private URL jarFileURL;
    private URLClassLoader classLoader;

    public AppSession(URI uri) throws IOException {
        JarURLConnection jarURLConnection = (JarURLConnection) uri.toURL().openConnection();
        jarFileURL = jarURLConnection.getJarFileURL();
        classLoader = new URLClassLoader(new URL[]{jarFileURL});
    }

    @Override
    public String getHost() {
        return jarFileURL.toString();
    }

    @Override
    public Class loadClass(URI uri) throws ClassLoadingException {
        return null;
//        try {
//            String path = "jar:" + uri.toString().replaceAll("app://", JBrowser.getRepository().toURI().toString());
//            URL url = new URL(path);
//            JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
//            if (jarURLConnection.getEntryName() != null && !jarURLConnection.getEntryName().isEmpty()) {
//                String className = jarURLConnection.getEntryName().replace("/", ".").replace(".class", "");
//                return Class.forName(className, true, classLoader);
//            } else {
//                String mainClass = jarURLConnection.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
//                return Class.forName(mainClass, true, classLoader);
//            }
//        } catch (Exception ex) {
//            throw new ClassLoadingException(ex);
//        }
    }
}
